Imports System.Xml.Serialization
Namespace SSDL

    Public Class Schema
        Inherits XEntityModelElementBase

        ''' <summary>Initializes an instance of the <see cref="Schema" /> class.
        ''' This is the default constructor for this class.</summary>
        Public Sub New()
            MyBase.New("Schema", SsdlXmlNamespace)
            Me.Add(New XAttribute("xmlns:store", EntityStoreSchemaGeneratorXmlNamespace))
            'MyBase.New(<Schema
            '               xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
            '               xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"/>)
        End Sub

        ''' <summary>Initializes an instance of the <see cref="Schema" /> class.</summary>
        ''' <param name="other">An <see cref="System.Xml.Linq.XElement"/> object to copy from.</param>
        Public Sub New(ByVal other As XElement)
            MyBase.New(other)
        End Sub

        Private __associations As AssociationCollection
        Private __entityTypes As EntityTypeCollection
        Private __functions As FunctionCollection

        <AutoLoad()> _
        <XmlElement("Association", GetType(Association))> _
        Public ReadOnly Property Associations() As AssociationCollection
            Get
                If (__associations Is Nothing) Then
                    __associations = New EdmxEditor.AssociationCollection(Me, MyBase.Name.Namespace, "Association")
                End If
                Return __associations
            End Get
        End Property

        <AutoLoad()> _
        <XmlElement("EntityType", GetType(EntityType))> _
        Public ReadOnly Property EntityTypes() As EntityTypeCollection
            Get
                If (__entityTypes Is Nothing) Then
                    __entityTypes = New EdmxEditor.SSDL.EntityTypeCollection(Me, MyBase.Name.Namespace, "EntityType")
                End If
                Return __entityTypes
            End Get
        End Property

        <Xml.Serialization.XmlIgnore()> _
        Public ReadOnly Property EntityType(ByVal name As String) As EntityType
            Get
                Return (__entityTypes.Where(Function(x) x.Name = name).FirstOrDefault())
            End Get
        End Property

        <AutoLoad()> _
        <XmlElement("EntityContainer")> _
        Public Property EntityContainer() As EntityContainer
            Get
                Return DirectCast(Me.Element(MyBase.Name.Namespace + "EntityContainer"), EdmxEditor.SSDL.EntityContainer)
            End Get
            Set(ByVal value As EntityContainer)
                If (DirectCast(value, XElement).Name <> (MyBase.Name.Namespace + "EntityContainer")) Then
                    Throw New ArgumentException("Invalid Element")
                End If
                If (Me.EntityContainer IsNot Nothing) Then
                    Me.EntityContainer.Remove()
                End If
                Me.Add(New EdmxEditor.SSDL.EntityContainer(value))
            End Set
        End Property

        <AutoLoad()> _
        <XmlElement("Function", GetType([Function]))> _
        Public ReadOnly Property Functions() As FunctionCollection
            Get
                If (__functions Is Nothing) Then
                    __functions = New EdmxEditor.SSDL.FunctionCollection(Me, MyBase.Name.Namespace, "Function")
                End If
                Return __functions
            End Get
        End Property

        <XmlAttribute()> _
        Public Property [Namespace]() As String
            Get
                Return Me.@Namespace
            End Get
            Set(ByVal value As String)
                Me.@Namespace = value
            End Set
        End Property

        <XmlAttribute()> _
        Public Property [Alias]() As String
            Get
                Return Me.@Alias
            End Get
            Set(ByVal value As String)
                Me.@Alias = value
            End Set
        End Property

        <XmlAttribute()> _
        Public Property Provider() As String
            Get
                Return Me.@Provider
            End Get
            Set(ByVal value As String)
                Me.@Provider = value
            End Set
        End Property

        <XmlAttribute()> _
        Public Property ProviderManifestToken() As String
            Get
                Return Me.@ProviderManifestToken
            End Get
            Set(ByVal value As String)
                Me.@ProviderManifestToken = value
            End Set
        End Property

    End Class

End Namespace